package com.example.a72443.dialog_box;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_dialog1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimpleDialog();
            }
        });
        findViewById(R.id.btn_dialog2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showselectDialog();
            }
        });
        findViewById(R.id.btn_dialog3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSingleDialog();
            }
        });
        findViewById(R.id.btn_dialog4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMultipleDialog();
            }
        });
        findViewById(R.id.btn_dialog5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCustomDialog();
            }
        });
    }

    private void showCustomDialog() {
        View view = LayoutInflater.from(MainActivity.this)
                .inflate(R.layout.dialog_input,null,false);

        final EditText nameEdit = view.findViewById(R.id.nameEdit);
        final EditText ageEdit = view.findViewById(R.id.ageEdit);

        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("你的姓名")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String name = nameEdit.getText().toString();
                        int age = Integer.parseInt(ageEdit.getText().toString());
                        Toast.makeText(MainActivity.this,
                                "你的姓名是"+name+"你的年龄"+age , Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    private void showMultipleDialog() {
        final String[]hobbies = {"篮球","足球","睡觉","听音乐"};
        final boolean isChecked[] = new boolean[6];
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择你的爱好")
                .setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        isChecked[i]=b;
                        if(b){
                            Toast.makeText(MainActivity.this,
                                    "你添加了"+hobbies[i],Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(MainActivity.this,
                                    "你取消了"+hobbies[i],Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        StringBuffer buffer = new StringBuffer();

                        for(int j=0;j<isChecked.length;j++){
                            if(isChecked[j]){
                                buffer.append(hobbies[j]);
                            }
                        }
                            Toast.makeText(MainActivity.this,
                                    "你的爱好有"+ buffer,Toast.LENGTH_SHORT).show();

                    }
                })
                .create()
                .show();
    }

    private void showSingleDialog() {
        final String[] sex = {"男","女","不知道"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择您的性别")
                .setSingleChoiceItems(sex, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "你的性别是"+sex[i],Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    private void showselectDialog() {
        final String []cities ={"beijing","shanghai","shenzhen","guangzhou"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("你的城市")
                .setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "你居住在"+cities[i],Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    private void showSimpleDialog() {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("提示")
                .setMessage("今天你生日")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "谢谢",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "哦",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("得了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "得了",Toast.LENGTH_SHORT).show();
                    }
                })

                .create()
                .show();

    }
}
